/**
 * Copyright (c) 2011 Daniel Centore
 *
 * ex: set filetype=java expandtab tabstop=4 shiftwidth=4 :
 * 
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 */
package wiiWired.controller.main;

import java.util.Arrays;

import javax.bluetooth.BluetoothStateException;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import motej.Mote;
import motej.MoteFinder;
import motej.MoteFinderListener;
import motej.centore.RotationController;
import wiiWired.controller.console.JBWindow;
import wiiWired.controller.console.wizard.MoteConnectionManager;
import wiiWired.controller.data.NameGetter;
import wiiWired.controller.mote.MoteController;
import wiiWired.controller.mote.TimerLights;

/**
 * Main class.
 * 
 * Accurate to 55 thousands of a second
 * 
 * Credits to the following:
 * http://bluecove.org/bluecove/apidocs/com/intel/bluetooth/BlueCoveImpl.html#useThreadLocalBluetoothStack()
 * http://markmail.org/message/7fbina4722qojywy#query:useThreadLocalBluetoothStack+page:1+mid:7fbina4722qojywy+state:results
 * http://motej.sourceforge.net/
 * http://bluecove.org/
 * 
 * @author Daniel Centore
 * 
 */
public class Main implements MoteFinderListener
{
	private static boolean allowNewConnections = false; // allows connections not in motes.txt
	
	public static void main(String args[]) throws BluetoothStateException
	{
		try
		{
			// Set System L&F
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e)
		{
			// handle exception
		} catch (ClassNotFoundException e)
		{
			// handle exception
		} catch (InstantiationException e)
		{
			// handle exception
		} catch (IllegalAccessException e)
		{
			// handle exception
		}

		//initialize Gui
		JBWindow.initialize();

		//Initialize other components
		RotationController.initializeDongles();
		TimerLights.initialize();

		//begin search for motes
		MoteFinder f = MoteFinder.getMoteFinder();
		f.addMoteFinderListener(new Main());

		f.startDiscovery();
	}

	@Override
	public void moteFound(Mote mote)
	{
		String bta = mote.getBluetoothAddress();

		if (!allowNewConnections && NameGetter.getMoteData(bta) == null)
		{
			System.out.println("REJECTED MOTE: " + bta);

			mote.setPlayerLeds(new boolean[] { true, true, true, true });
			mote.rumble(1000);

			try
			{
				Thread.sleep(1500);
			} catch (InterruptedException e)
			{
			}

			mote.setPlayerLeds(new boolean[] { false, false, false, false });

			mote.disconnect();

			return;
		}
		else if (NameGetter.getMoteData(bta) == null)
		{
			// add to motes.txt now
			String[][] data = NameGetter.getData();
			String[][] data2 = new String[data.length + 1][2];
			
			System.arraycopy(data, 0, data2, 0, data.length);
			data2[data2.length - 1][0] = bta;
			data2[data2.length - 1][1] = bta;
			
			NameGetter.writeToFile(data2);
			
			MoteConnectionManager.fireUpdateRequired();
		}

		new MoteController(mote);
	}

	public static boolean isAllowNewConnections()
	{
		return allowNewConnections;
	}

	public static void setAllowNewConnections(boolean allowNewConnections)
	{
		Main.allowNewConnections = allowNewConnections;
	}

}
